package com.xiaohao.server.parkmap.controller;

import com.xiaohao.server.parkmap.entity.Customer;
import com.xiaohao.server.parkmap.entity.ResponseResult;
import com.xiaohao.server.parkmap.entity.Scenic;
import com.xiaohao.server.parkmap.service.CustomerService;
import com.xiaohao.server.parkmap.service.ScenicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("scenic")
public class ScenicController {
    @Autowired
    private ScenicService scenicService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("{id}")
    public ResponseResult<Scenic> query(@PathVariable Integer id,
                                        @RequestParam(required = false) String wxOpenId) {
        if (Objects.nonNull(wxOpenId)) {
            Customer customer = customerService.queryOne(new Customer().setWxOpenId(wxOpenId));
            customer.setReward(customer.getReward() + 5);
            customerService.save(customer);
        }
        return ResponseResult.success(scenicService.queryById(id));
    }

    @GetMapping
    public ResponseResult<List<Scenic>> query(Scenic scenic) {
        return ResponseResult.success(scenicService.query(scenic));
    }

    @DeleteMapping("{id}")
    public ResponseResult<Void> delete(@PathVariable Integer id) {
        return scenicService.delete(id) > 0 ? ResponseResult.success() : ResponseResult.error();
    }

    @PostMapping
    public ResponseResult<Void> save(@RequestBody Scenic scenic) {
        return scenicService.save(scenic) > 0 ? ResponseResult.success() : ResponseResult.error();
    }
}
